﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Spike.Network;

namespace HelloSpikeClient
{
    class Program
    {
        // A flag which can be set to stop the program
        static bool Running = true;

        // A channel which represents a connection to the server
        static TcpChannel Channel = new TcpChannel();

        static void Main(string[] args)
        {
            // Wait a bit while our server starts...
            Thread.Sleep(5000);

            // Hook our Echo event
            Channel.EchoInform += OnEcho;

            // Connect to the server
            Channel.Connected += OnConnected;
            Channel.Connect("127.0.0.1", 8002);

            // Reception loop, can be in a separate thread
            Channel.ReceiveLoop(15, ref Running);
        }


        static void OnConnected(object sender, ConnectionEventArgs e)
        {
            Console.WriteLine("Connected!");

            // Send a message
            Channel.Echo("Hello, Spike World!");
        }

        static void OnEcho(object sender, PacketReceiveEventArgs<EchoInform> e)
        {
            Console.WriteLine("Received: " + e.Data.Text);
        }
    }
}
